﻿using System;
using System.Globalization;
using Succell.Dnn.JQuery;
using Succell.Dnn.JQuery.UI;
using DotNetNuke.Entities.Modules;

namespace Succell.Dnn.Modules.JQuery.Extras
{
    public partial class JQueryExtras : PortalModuleBase
    {
        // use this for debug version of jquery.
        //public override System.Web.UI.Page Page
        //{
        //    get
        //    {
        //        if (null != base.Page)
        //        {
        //            base.Page.ClientScript.RegisterClientScriptInclude(typeof (Dnn.JQuery.JQuery), "jquery-1.3.2",
        //                                                               VirtualPathUtility.ToAbsolute(
        //                                                                   "~/DesktopModules/Succell.JQuery/js/jquery-1.3.2.js"));
        //            Dnn.JQuery.JQuery.RegisterJQuery(base.Page);
        //            base.Page.ClientScript.RegisterClientScriptInclude(typeof (JQueryUI), "jquery-ui-1.7.2",
        //                                                               VirtualPathUtility.ToAbsolute(
        //                                                                   "~/DesktopModules/Succell.JQuery/js/jquery-ui-1.7.2.js"));
        //            base.Page.ClientScript.RegisterClientScriptInclude(typeof (Dnn.JQuery.Extras.WindowedDialog),
        //                                                               "jquery-ui.windowedDialog",
        //                                                               VirtualPathUtility.ToAbsolute(
        //                                                                   "~/DesktopModules/Succell.JQuery.Extras/js/jquery-ui.windowedDialog.0.5.0.js"));
        //        }
        //        return base.Page;
        //    }
        //    set { base.Page = value; }
        //}

        protected override void OnLoad(EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.txtThemeName.Text))
            {
                try
                {
                    JQueryUI.RegisterJQueryUITheme(this.Page, this.txtThemeName.Text);
                    JQueryUI.SetDefaultJQueryUITheme(this.Page, this.txtThemeName.Text);
                    this.lblThemeName.Text = "Theme: ";
                }
                catch (Exception)
                {
                    this.lblThemeName.Text = "<span style=\"color: red\">Theme does not exist.</span> Theme: ";
                }
            }

            base.OnLoad(e);

			string url = this.ResolveUrl("~/DesktopModules/Succell.JQuery.Extras/Dialog.aspx");
			this.wdlgModeless.Url = url;
			this.wdlgModal.Url = url;
			this.wdlgAnotherModal.Url = url;
			
            Dnn.JQuery.JQuery.RegisterJQueryOnDocumentReadyBlock(
                this.Page,
                String.Format(
                    CultureInfo.InvariantCulture,
                    @"$('#modeless-dialog').click(function(evt) {{ {0}; }});
$('#modal-dialog').click(function(evt) {{ {1}; }});
$('#another-modal-dialog').click(function(evt) {{ {2}; }});
",
                    this.wdlgModeless.ClientMethods.Open(),
                    this.wdlgModal.ClientMethods.Open(),
                    this.wdlgAnotherModal.ClientMethods.Open()));

            this.wmeKeyPress.Selector = Dnn.JQuery.JQuery.JQueryClientID(this.txtWatermarkedUsername);
            this.wmeFocus.Selector = Dnn.JQuery.JQuery.JQueryClientID(this.txtWatermarkedPassword);

            if (this.ModuleConfiguration.IsPremium)
            {
                this.lblPremiumMessage.Visible = false;
            }
        }
    }
}
